import java.io.*;
import java.lang.*;
import java.util.*;


public class Driver{

	
	public Driver(){

	}
	
	public static void main(String [] args){
		Thread [] tests;
		BufferedReader brIn;
		String temp = "";
		int i = 0;
		Pipe p = new Pipe();		
		tests = new Thread[20];
		brIn = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer t;
			System.out.println("Enter up to 20 tests in the format:");
			System.out.println(" read <int size> ");
			System.out.println(" write <String> ");
			System.out.println("Type \"run\" to start the tests");
			System.out.println("Type \"exit\" to exit the program");
		boolean b = true;
		while(b){
			String op = "";
			String op2 = "";
			System.out.print(">>");
			try{
				if(i==tests.length-1){
					System.out.println("maximum number of tests enters, please type \"run\" to run them all");
				}
				
				temp = brIn.readLine().trim();
				t = new StringTokenizer(temp);
				op = t.nextToken();
				
				
				if(op.equals("read")){
					System.out.println("adding..");
					op2 = t.nextToken();
					int size = (new Integer(op2)).intValue();
					if(i != tests.length-1){
						tests[i] = (new Reader(p,size));
						i++;
					}
				}
				if(op.equals("write")){
					System.out.println("adding...");
					for(int j=0;j<t.countTokens();j++){
						op2 += t.nextToken();
					}
					if(i != tests.length-1){
						tests[i] = (new Writer(p,op2));
						i++;
					}
				}
				if(op.equals("run")){
					System.out.println("Running\n-------------------------");
					for(int j=0;j<i;j++){
						tests[j].start();
					}
					break;
				}
				
				if(op.equals("exit")){
					System.exit(1);
				}
			
							
			}catch(Exception e){}

			
		}
			
		
	}	
}